from app.models import SQLBaseModel
from .base import IdType
from sqlmodel import Field, Relationship, SQLModel
from typing import TYPE_CHECKING, Optional

if TYPE_CHECKING:
    from .dict_type import DictType


class BaseDict(SQLModel):
    name: str = Field(nullable=False, index=True, description="名称")
    code: str = Field(nullable=False, index=True, description="编码")
    value: str = Field(nullable=False, index=True, description="编码")

    sort: int = Field(default=100, index=True, description="顺序")
    description: Optional[str] = Field(default=None, description="描述")
    is_enabled: bool = Field(default=True, index=True, description="是否启用")


class Dict(SQLBaseModel, BaseDict, table=True):
    __tablename__ = "dict"

    """一对多关系"""
    dict_type_id: Optional[IdType]= Field( default=None, index=True, foreign_key="dict_type.id", description="字典类型ID")
    dict_type: Optional["DictType"] | None = Relationship(back_populates="dicts")
